package com.utc.common.util;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.LayoutManager;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JViewport;

import com.teamcenter.rac.common.lov.HierarchicalLOVComponent;
import com.teamcenter.rac.common.lov.LOVComboBox;
import com.teamcenter.rac.common.lov.LOVUIComponent;
import com.teamcenter.rac.stylesheet.AbstractRendering;
import com.teamcenter.rac.stylesheet.PropertyCheckbox;
import com.teamcenter.rac.stylesheet.PropertyLOVUIComponent;
import com.teamcenter.rac.stylesheet.PropertyLogicalPanel;
import com.teamcenter.rac.stylesheet.PropertyNameLabel;
import com.teamcenter.rac.stylesheet.PropertyTextArea;
import com.teamcenter.rac.stylesheet.PropertyTextField;
import com.teamcenter.rac.util.iTextField;

public class RenderHandler {

	public RenderHandler() {

	}

	public static Container findPanel(AbstractRendering form) {
		Component temp = form.getComponent(0);
		while (temp.getClass().getName().compareTo(
				"com.teamcenter.rac.stylesheet.AutomaticRendering$BarPanel") != 0) {

			if (temp
					.getClass()
					.getName()
					.compareTo(
							"com.teamcenter.rac.stylesheet.XMLRendering$XMLContentHandler$BarPanel") == 0) {
				temp = ((Container) temp).getComponents()[0];
				return (Container) temp;
			}

			if (temp
					.getClass()
					.getName()
					.compareTo(
							"com.teamcenter.rac.stylesheet.AutomaticRendering$BarPanel") == 0) {
				return (Container) temp;
			}

			Component[] temps = ((Container) temp).getComponents();
			if (temps != null && temps.length > 0) {
				temp = temps[0];
				if (temp == null)
					break;
			} else
				break;
		}

		temp = form.getComponent(0);

		while (getLayout((Container) temp).indexOf("PropertyLayout") < 0) {
			Component temp1 = ((Container) temp).getComponent(0);
			if (temp1 == null)
				break;
			temp = temp1;
		}
		return (Container) temp;
	}

	private static String getLayout(Container temp) {
		LayoutManager lm = temp.getLayout();
		if (lm == null)
			return "";
		return lm.toString();
	}

	public static PropertyNameLabel findNameLabel(Container con, String str) {
		Component[] cmps = con.getComponents();
		if (cmps == null || cmps.length < 1)
			return null;
		for (Component cmp : cmps) {
			if (cmp instanceof PropertyNameLabel) {
				String prop = ((PropertyNameLabel) cmp).getProperty();
				if (prop.compareTo(str) == 0) {
					return (PropertyNameLabel) cmp;
				}
			}
		}
		return null;
	}

	public static JPanel findLOVJpanel(Container con, String str) {
		Component[] cmps = con.getComponents();
		if (cmps == null || cmps.length < 1)
			return null;
		for (Component cmp : cmps) {
			if (cmp instanceof JPanel) {
				Component lov = ((JPanel) cmp).getComponent(0);
				if (lov instanceof PropertyLOVUIComponent) {
					String prop = ((PropertyLOVUIComponent) lov).getProperty();
					if (prop.compareTo(str) == 0)
						return (JPanel) cmp;
				}
			}
		}
		return null;
	}

	public static LOVUIComponent findLOVUIComponent(Container con, String str) {
		Component[] cmps = con.getComponents();
		if (cmps == null || cmps.length < 1)
			return null;
		for (Component cmp : cmps) {
			if (cmp instanceof JPanel) {
				Component lov = ((JPanel) cmp).getComponent(0);
				if (lov instanceof PropertyLOVUIComponent) {
					String prop = ((PropertyLOVUIComponent) lov).getProperty();
					if (prop.compareTo(str) == 0)
						return (LOVUIComponent) lov;
				}
			}
		}
		return null;
	}

	public static LOVComboBox findLOVComboBox(Container con, String str) {
		Component[] cmps = con.getComponents();
		if (cmps == null || cmps.length < 1)
			return null;
		for (Component cmp : cmps) {
			if (cmp instanceof JPanel) {
				Component lov = ((JPanel) cmp).getComponent(0);
				if (lov instanceof PropertyLOVUIComponent) {
					String prop = ((PropertyLOVUIComponent) lov).getProperty();
					if (prop.compareTo(str) == 0)
						return (LOVComboBox) ((PropertyLOVUIComponent) lov)
								.getComponent(0);
				}
			}
		}
		return null;
	}

	public static boolean changeLOVListener(LOVComboBox cbox, MouseListener e) {
		Component[] cmps = cbox.getComponents();
		if (cmps == null || cmps.length < 1)
			return false;
		boolean flag = false;
		JButton btn = null;
		for (Component cmp : cmps) {
			if (cmp instanceof JButton) {
				btn = (JButton) cmp;
				MouseListener[] listeners = btn.getMouseListeners();
				for (MouseListener listener : listeners) {
					if (cmp.getClass().getName().compareTo(
							"com.teamcenter.rac.util.combobox.iComboBox$13") == 0) {
						btn.removeMouseListener(listener);
						flag = true;
					}
				}
			}
		}
		if (flag)
			btn.addMouseListener(e);
		return flag;
	}

	public static HierarchicalLOVComponent findHierarchicalLOVComponent(
			Container con, String str) {
		Component[] cmps = con.getComponents();
		if (cmps == null || cmps.length < 1)
			return null;
		for (Component cmp : cmps) {
			if (cmp instanceof JPanel) {
				Component lov = ((JPanel) cmp).getComponent(0);
				if (lov instanceof PropertyLOVUIComponent) {
					String prop = ((PropertyLOVUIComponent) lov).getProperty();
					if (prop.compareTo(str) == 0)
						return (HierarchicalLOVComponent) ((PropertyLOVUIComponent) lov)
								.getComponent(0);
				}
			}
		}
		return null;
	}

	public static iTextField findiTextField(Container con, String str) {
		Component[] cmps = con.getComponents();
		if (cmps == null || cmps.length < 1)
			return null;
		for (Component cmp : cmps) {
			if (cmp instanceof PropertyTextField) {
				String prop = ((PropertyTextField) cmp).getProperty();
				if (prop.compareTo(str) == 0) {
					return (iTextField) cmp;
				}
			}
		}
		return null;
	}

	public static PropertyLogicalPanel findLogicalPanel(Container con,
			String str) {
		Component[] cmps = con.getComponents();
		if (cmps == null || cmps.length < 1)
			return null;
		for (Component cmp : cmps) {
			if (cmp instanceof PropertyLogicalPanel) {
				String prop = ((PropertyLogicalPanel) cmp).getProperty();
				if (prop.compareTo(str) == 0) {
					return (PropertyLogicalPanel) cmp;
				}
			}
		}
		return null;
	}

	public static boolean getLogicalStatus(PropertyLogicalPanel LPanel) {
		Component[] chibi = LPanel.getComponents();
		for (Component cmt : chibi) {
			if (cmt instanceof JRadioButton) {
				if (((JRadioButton) cmt).isSelected()) {
					String txt = ((JRadioButton) cmt).getText().toUpperCase();
					if (txt.compareTo("TRUE") == 0)
						return true;
					if (txt.compareTo("FALSE") == 0)
						return false;
				}
			}
		}
		return false;
	}

	public static JRadioButton getLogicalButton(PropertyLogicalPanel LPanel,
			boolean stat) {
		Component[] chibi = LPanel.getComponents();
		String txt = stat ? "TRUE" : "FALSE";
		for (Component cmt : chibi) {
			if (cmt instanceof JRadioButton) {
				if (((JRadioButton) cmt).getText().toUpperCase().compareTo(txt) == 0) {
					return (JRadioButton) cmt;
				}
			}
		}
		return null;
	}

	public static void setLogicalStatus(PropertyLogicalPanel LPanel,
			boolean stat) {
		Component[] chibi = LPanel.getComponents();
		String txt = stat ? "TRUE" : "FALSE";
		for (Component cmt : chibi) {
			if (cmt instanceof JRadioButton) {
				if (((JRadioButton) cmt).getText().toUpperCase().compareTo(txt) == 0) {
					((JRadioButton) cmt).setSelected(true);
				}
			}
		}
	}

	public static void setColor(Container con) {
		Component[] cmps = con.getComponents();
		if (cmps == null || cmps.length < 1)
			return;
		for (Component cmp : cmps) {
			if (cmp instanceof PropertyTextField) {
				((PropertyTextField) cmp).setOpaque(true);
				((PropertyTextField) cmp).setBackground(Color.WHITE);
			}
			if (cmp instanceof JScrollPane) {
				Component temp = ((JScrollPane) cmp).getComponents()[0];
				if (temp instanceof JViewport) {
					temp = ((JViewport) temp).getComponents()[0];
					if (temp instanceof PropertyTextArea) {
						((PropertyTextArea) temp).setOpaque(true);
						((PropertyTextArea) temp).setBackground(Color.WHITE);
					}
				}
			}
		}
	}

	public static void setColor(Container con, Color co) {
		con.setBackground(Color.WHITE);
		Component[] cmps = con.getComponents();
		if (cmps == null || cmps.length < 1)
			return;
		for (Component cmp : cmps) {
			if (cmp != null) {
				cmp.setBackground(co);
				if (cmp instanceof Container)
					setColor((Container) cmp, co);
			}
		}
	}

	// ==============================================================================================//

	public static PropertyLOVUIComponent findPLOVUIComponent(Container con,
			String propname) {
		Component[] cmps = con.getComponents();
		if (cmps == null || cmps.length < 1)
			return null;
		for (Component cmp : cmps) {
			if (cmp instanceof PropertyLOVUIComponent) {
				String prop = ((PropertyLOVUIComponent) cmp).getProperty();
				if (prop.compareTo(propname) == 0) {
					return (PropertyLOVUIComponent) cmp;
				}
			}
		}
		return null;
	}

	public static PropertyCheckbox findPCheckbox(Container con, String propname) {
		Component[] cmps = con.getComponents();
		if (cmps == null || cmps.length < 1)
			return null;
		for (Component cmp : cmps) {
			if (cmp instanceof PropertyCheckbox) {
				String prop = ((PropertyCheckbox) cmp).getProperty();
				if (prop.compareTo(propname) == 0) {
					return (PropertyCheckbox) cmp;
				}
			}
		}
		return null;
	}

	public static PropertyTextField findPPropertyTextField(Container con,
			String propname) {
		Component[] cmps = con.getComponents();
		if (cmps == null || cmps.length < 1)
			return null;
		for (Component cmp : cmps) {
			if (cmp instanceof PropertyTextField) {
				String prop = ((PropertyTextField) cmp).getProperty();
				if (prop.compareTo(propname) == 0) {
					return (PropertyTextField) cmp;
				}
			}
		}
		return null;
	}

//	public static FotonRefList findFotonRefList(Container con, String pname) {
//		Component[] cmps = con.getComponents();
//		if (cmps == null || cmps.length < 1)
//			return null;
//		for (Component cmp : cmps) {
//			if (cmp instanceof FotonRefList) {
//				String name = ((FotonRefList) cmp).getName();
//				if (name.compareTo(pname) == 0) {
//					return (FotonRefList) cmp;
//				}
//			}
//		}
//		return null;
//	}

//	public static Vector<FotonRefList> findFotonRefLists(Container con) {
//		Vector<FotonRefList> vec = new Vector<FotonRefList>();
//		Component[] cmps = con.getComponents();
//		if (cmps == null || cmps.length < 1)
//			return null;
//		for (Component cmp : cmps) {
//			if (cmp instanceof FotonRefList) {
//				vec.add((FotonRefList) cmp);
//			}
//		}
//		return vec;
//	}
}